package threading;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo20 {
    public static void main(String[] args) {
        //阻塞队列
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        //消费者
        Thread t1 = new Thread(()->{
            while(true){
                try {
                    int value = blockingQueue.take();//从队列中取元素
                    System.out.println("消费元素： "+value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();

        //生产者
        Thread t2 = new Thread(()->{
            int value = 0;
            while(true){
                try {
                    System.out.println("产生元素： "+value);
                    blockingQueue.put(value);
                    value++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
    }
}
